<template>
  <div id="container">
    <el-form :model="loginForm" :rules="rules" ref="loginForm" label-width="100px" class="login-form">
      <h2 class="login-title">XX网</h2>
      <el-form-item label="用户姓名" prop="name">
        <el-input v-model="loginForm.name"></el-input>
      </el-form-item>
      <el-form-item label="用户密码" prop="pass">
        <el-input  v-model="loginForm.pass" show-password></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm('loginForm')">登陆</el-button>
        <el-button @click="resetForm('ruleForm')">重置</el-button>
      </el-form-item>
    </el-form>                     
  </div>
</template>

<script>

import dao from '@/api/dao'

export default {
  data() {
    return {
      loginForm:{
        name:'',
        pass:'',
      },
      rules: {
        name: [
      
          { required: true, message: "帐号不能为空", trigger: "blur" },
          { min: 2, max: 10, message: '长度必须在2到10位之间', trigger: 'blur' }
        ],
        pass: [
            { required: true, message: "密码不能为空", trigger: "blur" },
            { min: 4, max: 32, message: '长度必须在4到32位之间', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    resetForm(formName){
      this.$refs[formName].resetFields();
    },
    submitForm(formName){
      this.$refs[formName].validate((valid) =>{
        if(valid){
          dao.login(this.loginForm.name,this.loginForm.pass).then(response =>{
            const resp = response.data
            if(resp.flag){
              localStorage.setItem('etoak_cms_key', resp.data.username)
              if(resp.data.role === 1){
                this.$router.push('/home')
              }else{
                
                this.$router.push('/admin')
              } 
              this.$message({
                  message:'登陆成功',
                  type:'success',
              })
            }else{
              this.$message({
                message:resp.msg,
                type:'warning',
              })
            }
          })
        }else{
          this.$message({
            message:'登录失败！请查看验证条件',
            type:'warning',
          })
        }
      })
    }

  },
}
</script>

<style scoped>
.login-form {
  width: 350px;
  margin: 160px auto;
  background-color: rgb(255, 255, 255, 0.6);
  padding: 28px;
  border-radius: 60px;
}
#container {
  width: 1280px;
  position: absolute;
  width: 1530px;
  height: 720px;

  background: url("../../assets/back.jpeg");  
}
.login-title {
  color: #303133;
  text-align: center;
}
</style>